using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using VideoSeller.Core.Configuration;
using VideoSeller.Core.Interfaces;

namespace VideoSeller.Core.Localization
{
	internal static class SqlResourceHelper
	{
		private static IApplicationSettingsProvider _applicationSettingsProvider;
		private static IDatabaseSettingsProvider _databaseSettingsProvider;

		static SqlResourceHelper()
		{
			_applicationSettingsProvider = IoC.Resolve<IApplicationSettingsProvider>();
			_databaseSettingsProvider = IoC.Resolve<IDatabaseSettingsProvider>();
		}

        #region Get

        /// <summary>
        /// Gets the resources.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="className">Name of the class.</param>
        /// <param name="cultureName">Name of the culture.</param>
        /// <param name="designMode">if set to <c>true</c> [design mode].</param>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns></returns>
        public static IDictionary GetResources(string virtualPath, string className, string cultureName, bool designMode, IServiceProvider serviceProvider)
        {
            if (String.IsNullOrEmpty(virtualPath) && String.IsNullOrEmpty(className))
            {
                throw new Exception("SqlResourceHelper.GetResources() - virtualPath or className missing from parameters.");
            }
            return GetResources((!String.IsNullOrEmpty(virtualPath) ? virtualPath : className), cultureName, designMode, serviceProvider);
        }

        /// <summary>
        /// Gets the resources.
        /// </summary>
        /// <param name="resourceObject">The resource object.</param>
        /// <param name="cultureName">Name of the culture.</param>
        /// <param name="designMode">if set to <c>true</c> [design mode].</param>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns></returns>
        public static IDictionary GetResources(string resourceObject, string cultureName, bool designMode, IServiceProvider serviceProvider)
        {
			var sqlConnection = new SqlConnection(_applicationSettingsProvider.VideoSellerConnectionString);
			var sqlCommand = new SqlCommand();
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.CommandText = "GetResources";
            sqlCommand.Parameters.AddWithValue("@ResourceObject", resourceObject);
            sqlCommand.Parameters.AddWithValue("@CultureName", String.IsNullOrEmpty(cultureName) ? CultureInfo.CurrentUICulture.Name : cultureName);

			var showNumber = _databaseSettingsProvider.ShowResourceNumber;
			var resources = new ListDictionary();
            try
            {
                sqlCommand.Connection = sqlConnection;
                sqlConnection.Open();
				var dataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);

				while (dataReader.Read())
                {
	                if (showNumber)
	                {
		                resources.Add(dataReader.GetString(dataReader.GetOrdinal("ResourceName")),
		                              string.Format("{0}. {1}",
		                                            dataReader.GetInt32(dataReader.GetOrdinal("Id")),
		                                            dataReader.GetString(dataReader.GetOrdinal("ResourceValue"))));
	                }
	                else
	                {
						resources.Add(dataReader.GetString(dataReader.GetOrdinal("ResourceName")), dataReader.GetString(dataReader.GetOrdinal("ResourceValue")));
	                }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
            finally
            {
                if (sqlConnection.State == ConnectionState.Open) sqlConnection.Close();
            }

            return resources;
        }

        #endregion

        #region Put

        /// <summary>
        /// Adds the resource.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="resourceObject">The resource object.</param>
        /// <param name="cultureName">Name of the culture.</param>
        /// <param name="resourceValue">The resource value.</param>
        public static void AddResource(string resourceName, string resourceObject, string cultureName, string resourceValue)
        {
			var sqlConnection = new SqlConnection(_applicationSettingsProvider.VideoSellerConnectionString);
			var sqlCommand = new SqlCommand();
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.CommandText = "PutResource";
            sqlCommand.Parameters.AddWithValue("@ResourceName", resourceName);
            sqlCommand.Parameters.AddWithValue("@ResourceValue", resourceValue);
            sqlCommand.Parameters.AddWithValue("@CultureName", cultureName);
            sqlCommand.Parameters.AddWithValue("@ResourceObject", resourceObject);

            try
            {
                sqlCommand.Connection = sqlConnection;
                sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                if (sqlConnection.State == ConnectionState.Open) sqlConnection.Close();
            }
        }

        /// <summary>
        /// Adds the resource.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="className">Name of the class.</param>
        /// <param name="cultureName">Name of the culture.</param>
        /// <param name="resourceValue">The resource value.</param>
        public static void AddResource(string resourceName, string virtualPath, string className, string cultureName, string resourceValue)
        {
            string resourceObject = "UNKNOWN **ERROR**";
            if (!String.IsNullOrEmpty(virtualPath))
            {
                resourceObject = virtualPath;
            }
            else if (!String.IsNullOrEmpty(className))
            {
                resourceObject = className;
            }
            AddResource(resourceName, resourceObject, (String.IsNullOrEmpty(cultureName) ? CultureInfo.CurrentUICulture.Name : cultureName), resourceValue);
        }

        #endregion
    }
}